import requests
import json

headers = {'Content-Type': 'application/json;charset=UTF-8'}
data = {"title": "天龙八部", "desc": "武侠小说", "pub_date": "2020-12-12", "read": "100"}
pk = 0


# 创建 天龙八部
def post(url):
    global pk
    try:
        resp = requests.post(url, data=json.dumps(data), headers=headers, timeout=2)
    except requests.exceptions.ConnectTimeout as e:
        raise Exception('post:%s, 请求超时' % url)
    except Exception as e:
        raise Exception('post:%s, 请求失败' % url)
    if resp.status_code == 201:
        pass
    else:
        raise Exception('post:%s,响应码不是201' % url)
    try:
        resp_data = json.loads(resp.text)
    except Exception as e:
        raise Exception('post:%s, 响应体json.loads失败' % url)

    try:
        pk = resp_data['id']
    except Exception as e:
        raise Exception('post:%s, 创建图书没有返回id' % url)
    for k in data:
        try:
            d = resp_data[k]
        except Exception as e:
            raise Exception('post:%s, 没有返回%s键' % (url, k))
        if d == data[k]:
            pass
        else:
            raise Exception('post:%s, %s键,创建数据不正确' % (url, k))


# 查询 天龙八部
def search(url):
    params = {'title': '天龙八部'}
    try:
        resp = requests.get(url, params={'title': '天龙八部'})
    except Exception as e:
        raise Exception('search:%s, 请求失败' % url)

    if resp.status_code == 200:
        pass
    else:
        raise Exception('search:%s, 请求失败, 响应码不是200' % url)

    try:
        resp_data = json.loads(resp.text)
    except Exception as e:
        raise Exception('search:%s, 响应体json.loads失败' % url)

    for r in resp_data:
        try:
            d = r['title']
        except Exception as e:
            raise Exception('search:%s,没有返回"title"键' % url)

        if d == params['title']:
            pass
        else:
            raise Exception('search:%s,根据title查询图书,返回值title不正确' % url)


# 查询 天龙八部
def get(url, pk):
    try:
        resp = requests.get(url + str(pk) + '/')
    except Exception as e:

        raise Exception('get_pk:%s, 请求失败' % url)

    if resp.status_code == 200:
        pass
    else:
        raise Exception('get_pk:%s, 请求失败, 响应码不是200' % url)

    try:
        resp_data = json.loads(resp.text)
    except Exception as e:
        raise Exception('get_pk:%s, 返回数据不能解析成json' % url)

    try:
        d = resp_data['id']
    except Exception as e:
        raise Exception('get_pk:%s, 根据pk查询图书内容, id获取失败' % url)
    if resp_data['id'] == pk:
        pass
    else:
        raise Exception('get_pk:%s, 获取单个图书失败' % url)


def put(url, pk):
    new_data = {'title': '天龙八部后传'}
    try:
        resp = requests.put(url + str(pk) + '/', data=json.dumps(new_data), headers=headers)
    except Exception as e:
        raise Exception('put:%s, 请求失败' % url)

    if resp.status_code == 200:
        pass
    else:
        raise Exception('put:%s, 请求失败, 响应码不是200' % url)

    try:
        resp_data = json.loads(resp.text)
    except Exception as e:
        raise Exception('put:%s, 返回数据不能解析成json' % url)

    try:
        d = resp_data['title']
    except Exception as e:
        raise Exception('put:%s, 数据没有返回title' % url)

    if d == new_data['title']:
        pass
    else:
        raise Exception('put:%s, 修改title失败' % url)


def delete(url, pk):
    try:
        resp = requests.delete(url + str(pk) + '/')
    except Exception as e:
        raise Exception('delete:%s, 请求失败' % url)

    if resp.status_code == 204:
        pass
    else:
        raise Exception('delete:%s, 删除status_code不是204' % url)


if __name__ == '__main__':
    ips = [
        # {'ip':'192.168.0.1', 'name':'本机'},
        {'ip': '192.168.0.6', 'name': '吴志浩'},
        {'ip': '192.168.0.218', 'name': '颜梓钰'},
        {'ip': '192.168.0.81', 'name': '姬宏辰'},
        {'ip': '192.168.0.234', 'name': '袁飞'},
        {'ip': '192.168.0.188', 'name': '邹志强'},
        {'ip': '192.168.0.87', 'name': '余波'},
        {'ip': '192.168.0.83', 'name': '张昊'},
        {'ip': '192.168.0.109', 'name': '白文太'},
        {'ip': '192.168.0.144', 'name': '薛飞'},
        {'ip': '192.168.0.73', 'name': '丁伟超'},
        {'ip': '192.168.0.135', 'name': '李京泽'},
        {'ip': '192.168.0.136', 'name': '孙晓东'},
        {'ip': '192.168.0.189', 'name': '李珊珊'},
        {'ip': '192.168.0.168', 'name': '张金'},
        {'ip': '192.168.0.39', 'name': '李文博'},
        {'ip': '192.168.0.77', 'name': '吴娜'},
        {'ip': '192.168.0.78', 'name': '刘再昂'},
    ]
    for ip in ips:
        try:
            url = 'http://%s:8000/books/' % ip['ip']
            print(url, ip['name'])
            post(url)
            get(url, pk)
            search(url)
            put(url, pk)
            delete(url, pk)
        except Exception as e:
            print(e)
            continue
        else:
            print(url, ip['name'], '通过!')
